#ifndef CLIENTHANDLER_H
#define CLIENTHANDLER_H


// preprocessor directives
#include <iostream>
#include <QtCore>
#include <QTcpServer>
#include <QTcpSocket>
#include <QString>
#include <QDataStream>
#include <QVector>


class ClientHandler : public QObject
{
    Q_OBJECT

public:
     //QString remove;

    static ClientHandler *clientHandlerInstance();

    //    ClientHandler(QTcpSocket *clientSocket);
    void setClientSocket(QTcpSocket *clientSocket);
    void setUsername(QString username);
    void setPassword(QString passworkd);
    void setEmail(QString email);
    void setFrom(QString from);
    void setTo(QString to);
    void setRemove(QString *remove);
    void setPresence(QString presence);
    void setScreenName (QString screenName);
    void setResult (int result);
    void setClosedBuddyWindow(QString nameOfTheWindow);
    void setOutgoingMessage(QString outgoingMessage);
    QString getUsername();
    QString getFrom();
    QString getTo();
    QString getEmail();
    QString getIncomingMessage();
    QString getPresence();
    int getResult();
    void getRegistration(QDataStream &theData);
    QString getClosedWindow();
    void registerUser(QDataStream &theData);
    void getMessage(QDataStream &theData);
    void getBuddies();
    void sendAuthorization();
    void sendRegistration();
    void sendMessage();
    void sendAddContact();
    void sendPresence();
    void emitPrintingSignal();
    void emitClosedWindow();
    void sendRemoveContact();

    QVector<QString> allMyBuddies;
    QVector<QString> allMyBuddiesStatus;

private slots:

    /*! handleIncomingData
      \brief handles incoming data
      !/
    void handleIncomingData();

    /*! handleDisconnection
      \brief handles a disconnection
      */
    void handleDisconnection();

signals:

    /*! authorizationPassed
      \brief signal
      */
    void authorizationPassed();

    /*! authorizationDenied
      \brief signal
      */
    void authorizationDenied();

    /*! messageReady
      \brief signal
      */
    void messageReady();

    /*! messageReadyForDisplaying
      \brief signal
      */
    void messageReadyForDisplaying();

    /*! rosterReceived
      \brief signal
      */
    void rosterReceived();

    /*! statusReceived
      \brief signal
      */
    void statusReceived();
    void registrationPassed();
    void registrationDenied();
    void closedWindow();
    void closeAddWindow();


private:

    /*! ClientHandler
      \brief constructor
      */
    ClientHandler();

    /*! \var static ClientHandler *instance_p
      \brief pointer to instance of client handler
      (singleton design pattern)
      */
    static ClientHandler *instance_p;

    /*! \var QTcpSocket *mySocket
      \brief pointer to the user's socket
      */
    QTcpSocket *mySocket;

    /*! \var QString tag
      \brief the tag of the type of data being sent
      */
    QString tag;

    /*! \var QString username
      \brief the username
      */
    QString username;

    /*! \var QString email
      \brief the email
      */
    QString email;

    /*! \var  QString password
      \brief the password
      */
    QString password;

    /*! \var QString from
      \brief specifies from whom the message is being sent
      */
    QString from;

    /*! \var QString to
      \brief specifies to who the message is being sent
      */
    QString to;

    /*! \var QString status
      \brief Status of user
      */
    QString status;
    QString screenName;
    QString closedBuddyWindow;     // indicates the windows that is being closed
    QString incomingMessage;

    /*! \var QString outgoingMessage
      \brief indicates outgoing messages
      */
    QString outgoingMessage;

    /*! \var int userId
      \brief id of the user
      */
    int userId;
    int result;
    void tagReader(QDataStream &theData);

    /*! getAuthorization
      \brief gets the authorization info
      \param theData the data stream
      */
    void getAuthorization(QDataStream &theData);

    /*! setBuddyList
      \brief sends the buddylist usernames to UI
      \param theData the data stream
      */
    void setBuddyList(QDataStream &theData);

    /*! setBuddyList
      \brief sends the buddylist status to UI
      \param theData the data stream
      */
    void setBuddyListStatus(QDataStream &theData);

};

#endif // CLIENTHANDLER_H
